﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Confessor
{
    static class xTreeViewObject
    {
        public static void CreateTreeList(System.Windows.Forms.TreeView treeView, ScanObject scanObject)
        {
            int rootNodes = treeView.Nodes.Count;
            for (int iRootNode=0; iRootNode < rootNodes; iRootNode++)
            {
                if (treeView.Nodes[iRootNode].Checked)
                {
                    scanObject.treeViewNodesList.Add(iRootNode.ToString());
                }
                int childNode1 = treeView.Nodes[iRootNode].GetNodeCount(false);
                for (int iChild1 = 0; iChild1 < childNode1; iChild1++)
                {
                    if (treeView.Nodes[iRootNode].Nodes[iChild1].Checked)
                    {
                        scanObject.treeViewNodesList.Add(iRootNode + "." + iChild1);
                    }
                    int childNode2 = treeView.Nodes[iRootNode].Nodes[iChild1].GetNodeCount(false);
                    for (int iChild2 = 0; iChild2 < childNode2; iChild2++)
                    {
                        if (treeView.Nodes[iRootNode].Nodes[iChild1].Nodes[iChild2].Checked)
                        {
                            scanObject.treeViewNodesList.Add(iRootNode + "." + iChild1 + "." + iChild2);
                        }
                        int childNode3 = treeView.Nodes[iRootNode].Nodes[iChild1].Nodes[iChild2].GetNodeCount(false);
                        for (int iChild3 = 0; iChild3 < childNode3; iChild3++)
                        {
                            if (treeView.Nodes[iRootNode].Nodes[iChild1].Nodes[iChild2].Nodes[iChild3].Checked)
                            {
                                scanObject.treeViewNodesList.Add(iRootNode + "." + iChild1 + "." + iChild2 + "." + iChild3);
                            }
                            int childNode4 = treeView.Nodes[iRootNode].Nodes[iChild1].Nodes[iChild2].Nodes[iChild3].GetNodeCount(false);
                            for (int iChild4 = 0; iChild4 < childNode4; iChild4++)
                            {
                                if (treeView.Nodes[iRootNode].Nodes[iChild1].Nodes[iChild2].Nodes[iChild3].Nodes[iChild4].Checked)
                                {
                                    scanObject.treeViewNodesList.Add(iRootNode + "." + iChild1 + "." + iChild2 + "." + iChild3 + "." + iChild4);
                                }
                            }
                        }
                    }
                }
            }
        }

        public static void RestoreTreeList(System.Windows.Forms.TreeView treeView, ScanObject scanObject)
        {

            foreach (String s in scanObject.treeViewNodesList)
            {
                string[] sArray = s.Split('.');
                if (sArray.Length == 1)
                {
                    treeView.Nodes[Convert.ToInt16(sArray[0])].Checked = true;
                }
                if (sArray.Length == 2)
                {
                    treeView.Nodes[Convert.ToInt16(sArray[0])].Nodes[Convert.ToInt16(sArray[1])].Checked = true;
                }
                if (sArray.Length == 3)
                {
                    treeView.Nodes[Convert.ToInt16(sArray[0])].Nodes[Convert.ToInt16(sArray[1])].Nodes[Convert.ToInt16(sArray[2])].Checked = true;
                }
                if (sArray.Length == 4)
                {
                    treeView.Nodes[Convert.ToInt16(sArray[0])].Nodes[Convert.ToInt16(sArray[1])].Nodes[Convert.ToInt16(sArray[2])].Nodes[Convert.ToInt16(sArray[3])].Checked = true;
                }
            }
        }
    }
}
